package com.wizardClient;

import Validator.Validator;
import com.carpark.ejb.Model.AccountingdataModel;
import com.carpark.ejb.Model.ClientsModel;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;

/**
 *
 * @author Sawior
 */
public class ChooseAccountingController implements Initializable, ClientControlledScreen {
    @FXML
    private TextField name;
    @FXML
    private TextField nip;
    @FXML
    private TextField city;
    @FXML
    private TextField zipCode;
    @FXML
    private TextField street;
    @FXML
    private TextField houseNr;
    @FXML
    private TextField localNr;
    @FXML
    private ChoiceBox payerType;
    @FXML
    private TextField phone;
    @FXML
    private TextArea comment;
    @FXML
    private Label labelName;
    @FXML
    private Label labelNip;
    @FXML
    private Label labelCity;
    @FXML
    private Label labelZipCode;
    @FXML
    private Label labelStreet;
    @FXML
    private Label labelHouseNr;
    @FXML
    private Label labelLocalNr;
    @FXML
    private Label labelPayerType;
    @FXML
    private Label labelPhone;
    @FXML
    private Label labelComment;
    private ClientScreenController myController;
    private ArrayList<Node> inputs = new ArrayList<Node>();
    private ArrayList<Label> labels = new ArrayList<Label>();

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        inputs.add(name);
        inputs.add(city);
        inputs.add(zipCode);
        inputs.add(street);
        inputs.add(houseNr);
        inputs.add(localNr);
        inputs.add(nip);
        inputs.add(phone);
        inputs.add(payerType);
        inputs.add(comment);
        
        labels.add(labelName);
        labels.add(labelCity);
        labels.add(labelZipCode);
        labels.add(labelStreet);
        labels.add(labelHouseNr);
        labels.add(labelLocalNr);
        labels.add(labelNip);
        labels.add(labelPhone);
        labels.add(labelPayerType);
        labels.add(labelComment);
    }

    @Override
    public void setScreenParent(ClientScreenController screenParent) {
        myController = screenParent;
    }

    @FXML
    private void goToMain(ActionEvent event) {
        myController.setScreen(ClientScreensFramework.FIRST_SCREEN);
    }
    public void loadData(ClientsModel client) {
        this.name.setText(client.getName() + " " + client.getLastName());
        this.city.setText(client.getCity());
        this.zipCode.setText(client.getZipCode());
        this.street.setText(client.getStreet());
        this.houseNr.setText(client.getHouseNumber());
        Integer localNumber = client.getLocalNumber();
        if(localNumber != null){
            this.localNr.setText(client.getLocalNumber().toString());
        }
        String clientPhone = client.getPhone();
        if(clientPhone != null){
            this.phone.setText(client.getPhone());
        }
        String clientNip = client.getNipNumber();
        if(clientNip != null) {
            this.nip.setText(client.getNipNumber());
        }
    }
    
    public void clearData() {
        this.name.setText("");
        this.city.setText("");
        this.zipCode.setText("");
        this.street.setText("");
        this.houseNr.setText("");
        this.localNr.setText("");
        this.phone.setText("");
        this.nip.setText("");
        this.comment.setText("");
        this.payerType.setValue("");
    }
    
    public AccountingdataModel getAccountingData() {
        String name = this.name.getText();
        String city = this.city.getText();
        String zipCode = this.zipCode.getText();
        String street = this.street.getText();
        String houseNumber = this.houseNr.getText();
        String nip = this.nip.getText();
        String payerType = translatePayerTypes((String) this.payerType.getValue());
        String localNumber = this.localNr.getText();
        String phone = this.phone.getText();
        String comment = this.comment.getText();
        AccountingdataModel account = new AccountingdataModel(name, city, zipCode, street, houseNumber, nip, payerType);
        account.setLocalNumber(localNumber);
        account.setPhone(phone);
        account.setComments(comment);

        return account;
    }

    @Override
    public boolean validateForm() {
        List<Boolean> checks = Validator.checkAccountingDataCheckList(getAccountingData());
        setWrongTextFields(checks);
        return isCorrect(checks);
    }
    
    private String translatePayerTypes(String payerType) {
        if(payerType == null) {
            return "";
        }
        if (payerType.equals("Osoba prywatna")) {
            return "PrivatePerson";
        }
        if (payerType.equals("Firma")) {
            return "Company";
        }
        if (payerType.equals("PrivatePerson")) {
            return "Osoba prywatna";
        }
        if (payerType.equals("Company")) {
            return "Firma";
        }
        return payerType;
    }
    
    public void setWrongTextFields(List<Boolean> textFieldsChecks) {
        int i = 0;
        for (Boolean isGood : textFieldsChecks) {
            if (isGood) {
                inputs.get(i).getStyleClass().remove("textFieldWrong");
                inputs.get(i).getStyleClass().add("textField");
                labels.get(i).setText("");
            } else {
                inputs.get(i).getStyleClass().remove("textField");
                inputs.get(i).getStyleClass().add("textFieldWrong");
                labels.get(i).setText("Złe dane");
            }
            i++;
        }
    }
    
    private boolean isCorrect(List<Boolean> list) {
        for (Boolean el : list) {
            if (!el) {
                return false;
            }
        }
        return true;
    }
    
}